package net.orange.architecture.web;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.orange.framework.model.User;

/**
 * Web parameters , constants and functions.
 */
public class Web {

	public final static String CHARACTER_ENCODING = "UTF-8";

	public final static String JSF_VIEWS_DIR = "/mgr/views";

	public final static String JSF_NAVIGATION_SEARCH = "search";

	public final static String JSF_NAVIGATION_DELETE = "delete";

	public final static String JSF_NAVIGATION_CREATE = "create";

	public final static String JSF_NAVIGATION_EDIT = "edit";

	public final static String JSF_NAVIGATION_OPEN = "open";

	public final static String JSF_NAVIGATION_COPY = "copy";

	public final static String JSF_NAVIGATION_SAVE = "save";

	public final static String JSF_NAVIGATION_NEW = "new";

	public final static String JSF_NAVIGATION_RETRY = "retry";

	public final static String JSF_NAVIGATION_BACK = "back";

	public final static String JSF_NAVIGATION_CONSOLE_EXECUTE = "execute";

	public final static String JSF_NAVIGATION_CONSOLE_EXPORT = "export";

	public final static String JSF_USER_LOGIN_SUCCESS = "success";

	public final static String JSF_USER_LOGIN_FAIL = "fail";

	public final static String JSF_USER_LOGOUT = "logout";

	public final static String JSF_USER_CURRENT = "currentUser";

	public final static String JSF_USER_CURRENT_MENUS = "currentUserMenus";

	/**
	 * Get the servlet context
	 * 
	 * @return The servet context
	 */
	public static ServletContext getServletContext() {
		return (ServletContext) FacesContext.getCurrentInstance()
				.getExternalContext().getContext();
	}

	/**
	 * Get the parameter value.
	 * 
	 * @param name
	 *            The request parameter name
	 * @return The parameter value.
	 */
	public static Object getParameter(String name) {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get(name);
	}

	/**
	 * Get the parameter array values.
	 * 
	 * @param name
	 *            name The request parameter name
	 * @return The parameter values.
	 */
	public static String[] getParameters(String name) {
		return (getRequest() != null) ? getRequest().getParameterValues(name)
				: null;
	}

	/**
	 * Get the servlet request.
	 * 
	 * @return The servlet request.
	 */
	public static HttpServletRequest getRequest() {
		if (FacesContext.getCurrentInstance() != null
				&& FacesContext.getCurrentInstance().getExternalContext() != null)
			return (HttpServletRequest) FacesContext.getCurrentInstance()
					.getExternalContext().getRequest();
		return null;
	}

	/**
	 * Get the http session
	 * 
	 * @return The http session
	 */
	public static HttpServletResponse getResponse() {
		return (HttpServletResponse) FacesContext.getCurrentInstance()
				.getExternalContext().getResponse();
	}

	/**
	 * Get the http session
	 * 
	 * @return The http session
	 */
	public static HttpSession getSession() {
		return (getRequest() != null) ? getRequest().getSession() : null;
	}

	/**
	 * Get the request attribute object.
	 * 
	 * @param name
	 *            The attribute object name
	 * @return The request attribute object.
	 */
	public static Object getAttribute(String name) {
		return getRequest().getAttribute(name);
	}

	/**
	 * Get the request value.
	 * 
	 * @param s0
	 *            The request parameter form name
	 * @param s1
	 *            The request parameter field name
	 * @return The request value.
	 */
	public static String getParameter(String s0, String s1) {
		String full = getFullParameterName(s0, s1);
		return (String) FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get(full);

	}

	/**
	 * Get the full request parameter name.
	 * 
	 * @param s0
	 *            The request parameter form name
	 * @param s1
	 *            The request parameter field name
	 * @return
	 */
	public static String getFullParameterName(String s0, String s1) {
		return ((s0 == null) ? "" : s0) + ":" + ((s1 == null) ? "" : s1);
	}

	/**
	 * Get the simple request parameter name (removed the form name).
	 * 
	 * @param s0
	 *            The request parameter name.
	 * @return the simple request parameter name (removed the form name).
	 */
	public static String getSimpleParameterName(String s0) {
		if (s0 != null)
			return s0.substring(s0.lastIndexOf(":") + 1, s0.length()).trim();
		else
			return null;
	}

	/**
	 * Get the request parameter map.
	 * 
	 * @return The request parameter map.
	 */
	public static Map getRequestParameterMap() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
	}

	/**
	 * Add information message to a sepcific client.
	 * 
	 * @param clientId
	 *            the client id
	 * @param msg
	 *            the information message
	 */
	public static void setMessage(String clientId, String msg) {
		FacesContext.getCurrentInstance().addMessage(clientId,
				new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg));
	}

	/**
	 * Get the current user in session.
	 */
	public static User getCurrentUser() {
		if (getSession() != null)
			return (User) getSession().getAttribute(JSF_USER_CURRENT);
		else
			return null;
	}

	/**
	 * Return the client language
	 * 
	 * @return The client language.
	 */
	public static String getClientLanguage() {

//		String lang = getRequest().getHeader("Accept-Language");
//		if (lang == null || lang.length() <= 0)
			return "en";
		//if (!lang.startsWith("zh"))
//			return lang.substring(0, 2);
//		else
//			return (lang.replace("zh-cn", "zh_CN")).replace("zh-tw", "zh_TW")
//					.replace("zh-CN", "zh_CN").replace("zh-TW", "zh_TW");
	}
}